//========================================================================================
//  
//  $File:$
//  Owner: Adobe Developer Technologies
//  
//  $Author: alokumar $
//  
//  $DateTime: 2011/04/01 11:22:15 $
//  
//  $Revision: #1 $
//  
//  $Change: 781912 $
//  
//  Copyright 1997-2010 Adobe Systems Incorporated. All rights reserved.
//  
//  NOTICE:  Adobe permits you to use, modify, and distribute this file in accordance 
//  with the terms of the Adobe license agreement accompanying it.  If you have received
//  this file from a source other than Adobe, then your use, modification, or 
//  distribution of it requires the prior written permission of Adobe.
//  
//========================================================================================

#include "VCPlugInHeaders.h"

// Interface includes:
#include "ISession.h"
#include "IIdleTask.h"
#include "IObserver.h"
#include "IApplication.h"

// General includes:
#include "CPMUnknown.h"

// Project includes:
#include "AMPID.h"
#include "IStartupShutdownService.h"
#include "AMGlobal.h"
#include "AMPLogDataRecord.h"
/** Implements IStartupShutdownService; purpose is to install the idle task.

	
	@ingroup paneltreeview
*/
class AMPStartupShutdown : 
	public CPMUnknown<IStartupShutdownService>
{
public:
	/**	Constructor
		@param boss interface ptr from boss object on which this interface is aggregated.
	 */
    AMPStartupShutdown(IPMUnknown* boss );

	/**	Destructor
	 */
	virtual ~AMPStartupShutdown() {}

	/**	Called by the core when app is starting up
	 */
    virtual void Startup();

	/**	Called by the core when app is shutting down
	 */
    virtual void Shutdown();
};

/* CREATE_PMINTERFACE
 Binds the C++ implementation class onto its ImplementationID 
 making the C++ code callable by the application.
*/
CREATE_PMINTERFACE(AMPStartupShutdown, kAMPStartupShutdownImpl)

/* Constructor
*/
AMPStartupShutdown::AMPStartupShutdown(IPMUnknown* boss) :
    CPMUnknown<IStartupShutdownService>(boss)
{
}

//bool16 IStartupServer = kFalse;
/********************************************************************************************************************
 *****Function: Startup
 *****Creator:  Toan Nguyen
 *****IN: 
 *****out: 
 *****Objective: start up idle service
 ********************************************************************************************************************/
void AMPStartupShutdown::Startup()
{
    do 
	{
		cout << "Audience Media Indesign Plugin Release Date: 2013-01-28" << endl;
		AMPUtils::InitSessionCounter();
		//InitLogCenter();
		InterfacePtr<IIdleTask> 
		task(GetExecutionContextSession(), IID_IPNLTRVIDLETASK);
		ASSERT(task);
		if(!task)
		{
			break;
		}
		task->InstallTask(0);
			
		//-->
		InterfacePtr<IIdleTask> 
		task2(GetExecutionContextSession(), IID_IAMPREQUESTINGTASK);
		ASSERT(task2);
		if(!task2)
		{
			break;
		}
		task2->InstallTask(1);
		//<--
		
		InterfacePtr<IApplication> app(GetExecutionContextSession()->QueryApplication());
		//if (app != nil) {
		//	cout << "app interfacePtr created" << endl;
		//}
		InterfacePtr<IObserver> iMyObserver(app, IID_SELECTIONOBSERVER);
		if (iMyObserver != nil) {
			//cout << "attach iDocObserver" << endl;
			iMyObserver->AutoAttach();
		}
		
		
	} while(kFalse);
}


/********************************************************************************************************************
 *****Function: Shutdown
 *****Creator:  Toan Nguyen
 *****IN: 
 *****out: 
 *****Objective: shutdown idle service
 ********************************************************************************************************************/
void AMPStartupShutdown::Shutdown()
{
	do 
	{
		InterfacePtr<IIdleTask> 
			task(GetExecutionContextSession(), IID_IPNLTRVIDLETASK);
		ASSERT(task);
		if(!task)
		{
			break;
		}
		task->UninstallTask();
		//-->
		InterfacePtr<IIdleTask> 
		task2(GetExecutionContextSession(), IID_IAMPREQUESTINGTASK);
		ASSERT(task2);
		if(!task2)
		{
			break;
		}
		task2->UninstallTask();
		//<--
		
		InterfacePtr<IApplication> app(GetExecutionContextSession()->QueryApplication());
		InterfacePtr<IObserver> iMyObserver(app, IID_SELECTIONOBSERVER);
		if (iMyObserver != nil) {
			//cout << "attach iDocObserver" << endl;
			iMyObserver->AutoDetach();
		}
		
	} while(kFalse);
}

/*
void AMPUtils::StartupShutdownIdleTask(bool Startup)
{
    do 
	{
		if(Startup == FALSE && IStartupServer == kTrue) {		// shutdown
			//-->
			InterfacePtr<IIdleTask> 
			task2(GetExecutionContextSession(), IID_IAMPREQUESTINGTASK);
			ASSERT(task2);
			if(!task2)
			{
				break;
			}
			task2->UninstallTask();
			IStartupServer = kFalse;
			
			//<--
			InterfacePtr<IApplication> app(GetExecutionContextSession()->QueryApplication());
			//if (app != nil) {
			//	cout << "app interfacePtr created" << endl;
			//}
			InterfacePtr<IObserver> iMyObserver(app, IID_SELECTIONOBSERVER);
			if (iMyObserver != nil) {
				//cout << "attach iDocObserver" << endl;
				iMyObserver->AutoAttach();
			}
			
		}else if (Startup == TRUE && IStartupServer == kFalse){						// start
			//-->
			InterfacePtr<IIdleTask> 
			task2(GetExecutionContextSession(), IID_IAMPREQUESTINGTASK);
			ASSERT(task2);
			if(!task2)
			{
				break;
			}
			task2->InstallTask(1);
			IStartupServer = kTrue;
			
			//<--
			InterfacePtr<IApplication> app(GetExecutionContextSession()->QueryApplication());
			//if (app != nil) {
			//	cout << "app interfacePtr created" << endl;
			//}
			InterfacePtr<IObserver> iMyObserver(app, IID_SELECTIONOBSERVER);
			if (iMyObserver != nil) {
				//cout << "attach iDocObserver" << endl;
				iMyObserver->AutoAttach();
			}
		}
	} while(kFalse);
}
*/